package com.news.service.impl;

import com.news.dao.ArticleDao;
import com.news.dao.UserDao;
import com.news.data.entity.Article;
import com.news.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Author : xuzhiyong
 * Date : 2022/5/23 11:03
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Override
    public List<Article> findAllArticle() {
        return articleDao.findAll();
    }

    @Override
    public boolean addArticle(Article article) {
        boolean flag = false;
        try {
            articleDao.save(article);
            flag = true;
        }catch (Exception e){
            System.out.println("添加新闻失败");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean updateArticle(Article article) {
        boolean flag = false;
        try {
            articleDao.save(article);
            flag = true;
        }catch (Exception e){
            System.out.println("修改失败！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteArticleById(Integer id) {
        boolean flag = false;
        try {
            articleDao.deleteById(id);
            flag = true;
        }catch (Exception e){
            System.out.println("删除失败！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Article findArticleById(Integer id) {
        try {
            Optional<Article> article = articleDao.findById(id);
            if(article.isPresent())
                return article.get();
        }catch (Exception e){
            System.out.println("查询失败！");
            e.printStackTrace();
        }
        return null;
    }
}
